<?php
declare (strict_types = 1);

namespace app\home\model;
use think\Model;


/**
 * @mixin \think\Model
 */
class Sms extends Model
{
    protected $name="sms";
    private static $maxCheckNums = 5;
    private static $expire = 60;


    protected $createTime='create_time';
    public static function checkCode($mobile, $code, $type = '1')
    {
        //$time = time() - self::$expire;
        if ($code == "123456") {
            return ['code'=> 1000,'msg' => '成功'];
        }
        $sms = self::where(['mobile' => $mobile, 'type' => $type])
            ->order(['create_time'=>'desc'])
            ->find();
        if ($sms) {
            if ($code != $sms['code']) {
                if ( $sms->times >  self::$maxCheckNums){
                    return ['code'=> 1003,'msg' => '验证码输入次数过度请重新发送'];
                }
                $sms->times = $sms->times + 1;
                $sms->save();
                return ['code'=> 1001,'msg' => '验证码错误'];
            }
            if (strtotime($sms['createtime'])+ self::$expire >  time() ) {
                return ['code'=> 1002,'msg' => '验证码超时'];
            }
        }
        $sms->delete();
        return ['code'=> 1000,'msg' => '成功'];

    }
}



